<template>
  <div>
    <common-head title="商品列表" />
    <div class="wrap">
      <ul class="nav-wrap">
        <li @click="switchTab(0)" :class="{ active: active === 0 }">综合</li>
        <li @click="switchTab(1)" :class="{ active: active === 1 }">销量</li>
        <li @click="orderByPrice" :class="['price', { active: active === 2 }]">
          <span>价格</span>
          <div
            :class="[
              'angle',
              'up',
              {
                active: (active === 2) & (priceUpDown === 0)
              }
            ]"
          ></div>
          <div
            :class="[
              'angle',
              'down',
              {
                active: (active === 2) & (priceUpDown === 1)
              }
            ]"
          ></div>
        </li>
        <li @click="switchTab(3)" :class="{ active: active === 3 }">新品</li>
      </ul>
      <div class="items">
        <div
          class="item"
          v-for="(item, index) in items"
          :key="item.id"
          @click="clickItem(item.id, index)"
        >
          <img :src="item.pic" class="img" alt="" />
          <div class="item-info">
            <h5 class="name">{{ item.name }}</h5>
            <p class="desc">
              {{ item.characteristic ? item.characteristic : "暂无描述" }}
            </p>
            <div class="acion">
              <p class="price">￥{{ item.minPrice }}</p>
              <van-icon size="25" class-prefix="icon" name="shop" />
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import CommonHead from 'components/CommonHead'
import { fetchItems } from 'api'
export default {
  name: 'itemList',
  data () {
    return {
      active: 0,
      priceUpDown: 0,
      items: []
    }
  },
  created () {
    this.fetchItems()
  },
  methods: {
    clickItem (id) {
      this.$router.push({
        path: '/detail',
        query: {
          id
        }
      })
    },
    fetchItems () {
      fetchItems({
        categoryId: this.$route.params.cateId,
        orderBy: this.orderBy
      }).then(res => {
        if (res.data.code === 0) {
          this.items = res.data.data.result
        }
      })
    },
    switchTab (index) {
      this.active = index
      this.fetchItems()
    },
    orderByPrice () {
      if (this.active !== 2) {
        this.active = 2
        this.priceUpDown = 0
      } else {
        if (this.priceUpDown === 0) {
          this.priceUpDown = 1
        } else {
          this.priceUpDown = 0
        }
      }
      this.fetchItems()
    }
  },
  computed: {
    orderBy () {
      let order = ''
      switch (this.active) {
        case 0:
          order = ''
          break
        case 1:
          order = 'ordersDown'
          break
        case 2:
          if (this.priceUpDown === 0) {
            order = 'priceUp'
          } else {
            order = 'priceDown'
          }
          break
        default:
          break
      }
      return order
    }
  },
  components: {
    CommonHead
  }
}
</script>

<style lang="scss" scoped>
.wrap {
  .nav-wrap {
    line-height: 44px;
    background-color: #fff;
    display: flex;
    font-size: 14px;
    color: #333;
    justify-content: space-around;
    li.active {
      color: #ff734c;
    }
    .price {
      position: relative;
      .angle {
        width: 0;
        height: 0;
        border: 6px solid transparent;
        position: absolute;
        right: -15px;
        &.up {
          border-bottom-color: #ccc;
          top: 10px;
          &.active {
            border-bottom-color: #ff734c;
          }
        }
        &.down {
          border-top-color: #ccc;
          bottom: 8px;
          &.active {
            border-top-color: #ff734c;
          }
        }
      }
    }
  }
}
.items{
  .item{
    height: 230px;
    margin:8px 0;
    display: flex;
    background-color: #fff;
    border-radius: 8px;
    overflow: hidden;
    align-items: center;
    .img{
      width: 180px;
      height: 180px;
    }
    .item-info{
      flex:1;
      height: 100%;
      padding-top: 20px;
      padding-left: 5px;
      .name{
        font-size: 16px;
        font-weight: 600;
        color: #333;
        margin-top: 10px;
        line-height: 20px;
      }
      .desc{
        width: 100%;
        font-size: 12px;
        color: #666;
        margin-top: 20px;
        line-height: 20px;
      }
      .acion{
        display: flex;
        margin-top: 60px;
        padding-right:10px;
        height: 50px;
        align-items: center;
        justify-content: space-between;
        font-size: 16px;
        color: #ff734c;
        .price{
          font-size: 16px;
          font-weight: 600;
          color: #232628;
        }
      }
    }
  }
}
</style>
